#include <bits/stdc++.h>
// 2025/06/11
// tag: 
// Author: Forever
using namespace std;
using int64 = int64_t;


#define int long long
const int N = 100001;

int n, cows, c[N], s[N], f[N], deep[N];
vector<pair<int, int>> edge[N];

void dfs(int u, int fa){
    for (auto [v, w] : edge[u]){
        if (v == fa) continue;
        deep[v] = deep[u] + w;
        dfs(v, u);
        s[u] += s[v];
    }
    s[u] += c[u];
}

void dfs2(int u, int fa){
    for (auto [v, w] : edge[u]){
        if (v == fa) continue;
        f[v] = f[u] + (cows - 2 * s[v]) * w;
        dfs2(v, u);
    }
}

signed main()
{
    ios::sync_with_stdio(false);
    cin.tie(0);
    cout.tie(0);
    cin >> n;
    for (int i = 1; i <= n; i++) 
        cin >> c[i], cows += c[i];
    for (int i = 1; i < n; i++){
        int a, b, l;
        cin >> a >> b >> l;
        edge[a].push_back({b, l});
        edge[b].push_back({a, l});
    }
    dfs(1, 0);
    for (int i = 1; i <= n; i++)
        f[1] += c[i] * deep[i];
    dfs2(1, 0);
    int ans = __LONG_LONG_MAX__;
    for (int i = 1; i <= n; i++)
        ans = min(ans, f[i]);
    cout << ans << endl;
    return 0;
}